export class DownloadByIframe {
  constructor (downloadBtnId) {
    this.init(downloadBtnId)
  }

  async init (id) {
    await this.createIframe(id)
  }

  createIframe (id) {
    const btn = document.getElementById(id)
    const iframe = document.createElement('iframe')
    iframe.width = 0
    iframe.height = 0
    iframe.frameborder = 0
    iframe.name = id
    iframe.style.display = 'none'
    iframe.appendChild(btn)
  }

  download (src) {
    function sa () {
      if (window.frames.exportIframe.document.readyState !== 'complete') {
        setTimeout(sa(), 100)
      } else {
        window.frames.exportIframe.document.execCommand('SaveAs')
      }
    }
    window.frames.exportIframe.location.href = src
    sa()
  }
}
